<%--
  Created by IntelliJ IDEA.
  User: 494418174
  Date: 2020/3/19 0019
  Time: 12:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">商品名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="productName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入商品名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">添加人:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="adminName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入添加人" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">分类名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <select name="categoryId"  class="layui-input-inline" style="height: 30px;border-radius: 10px">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">起始价格:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="number" name="minPrice" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入起始价格" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束价格:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="number" name="maxPrice" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入结束价格" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline" >
            <button type="button" class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm " lay-submit lay-filter="formDemo">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>

</form>
<!-- 搜索条件结束 -->

<table id="productTable" lay-filter="productTable"></table>

<script id="toolbarProduct" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="productAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="productDelChk">批量删除</button>
    </div>
</script>

<script id="barProduct" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
</script>

<%-- 添加或者修改弹出框开始 --%>
<div style="display: none" id="addOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline" id="d1">
                <label class="layui-form-label">商品编号:</label>
                <div class="layui-input-inline" style="padding: 5px">
                    <input type="text" name="productId" id="productId" autocomplete="off" class="layui-input layui-input-inline"
                           placeholder="请输入分类编号" style="height: 30px;border-radius: 10px" >
                </div>
            </div>
            <label class="layui-form-label">商品名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="productName" id="productName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入商品名称" style="height: 30px;border-radius: 10px" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline" id="d2">
                <label class="layui-form-label">分类名称:</label>
                <div class="layui-input-inline" style="padding: 5px">
                    <select name="categoryId" id="categoryId" class="layui-input-inline" style="height: 30px;border-radius: 10px">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <label class="layui-form-label">商品价格:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="number" name="price" id="price" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入商品价格" style="height: 30px;border-radius: 10px" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">

            <label class="layui-form-label">描述:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="description" id="description" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入商品描述" style="height: 30px;border-radius: 10px;width: 400px" >
            </div>


        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline" style="padding: 5px">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1" src="" style="width: 150px;height: 125px">
                        <p id="demoText"></p>
                        <input type="hidden" name="photo" id="photo">
                    </div>
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>

<script src="/layui/layui.js"></script>
<script>
    var productTable
    var index
    var url
    var uploadImg;
    layui.use(['table','form','layer','jquery','upload'], function() {
        var table = layui.table
        var form = layui.form
        var layer = layui.layer
        var $ = layui.jquery
        var upload=layui.upload

        // 初始table
        productTable = table.render({
            elem: "#productTable"
            , url: "/adminProduct/findProduct"
            , page: {limit: 5, limits: [5, 10, 15]}
            , cellMinWidth: 80
            , toolbar: '#toolbarProduct'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'productId', title: '商品编号', fixed: 'center'}
                , {field: 'productName', title: '商品名称', fixed: 'center'}
                , {field: 'categoryId', title: '分类id', fixed: 'center',hide:true}
                , {field: 'categoryName', title: '分类名称', fixed: 'center'}
                , {field: 'description', title: '商品描述', fixed: 'center'}
                , {field: 'price', title: '商品价格', fixed: 'center'}
                , {field: 'photo', title: '图片', fixed: 'center'}
                , {field: 'adminName', title: '添加人', fixed: 'center'}
                , {field: 'createTime', title: '添加时间', fixed: 'center'}
                , {fixed: 'right', title: '操作', toolbar: '#barProduct', width: 250}
            ]]
        })

         initCategory()
        // 查询
        form.on("submit(formDemo)", function (data) {
            productTable.reload({
                url: "/adminProduct/findProduct"
                , where: {

                    'productName':data.field.productName
                    ,'adminName':data.field.adminName
                    ,'categoryId':data.field.categoryId
                    ,'minPrice':data.field.minPrice
                    ,'maxPrice':data.field.maxPrice

                }
                , page: {
                    curr: 1
                }
            })
            return false;
        })
        // 头部工具栏添加事件
        table.on('toolbar(productTable)',function(obj) {
            if(obj.event == "productAdd"){ // 点击新增按钮执行的内容
                productAdd()
            }else if(obj.event=="productDelChk"){
                productDelChk(obj)
            }
        })
        //监听行工具事件
        table.on('tool(productTable)', function(obj){
            var data = obj.data
            if(obj.event == "edit"){
                menuUpdate(data)
            }else  if(obj.event == "del"){
                del(data)
            }
        })
        //图片上传
        uploadImg=upload.render({
            elem:"#test1"
            ,url:"/adminProduct/uploadImg"
            ,acceptMime:"image/*"
            ,done:function (res) {
                if(res.code>0){
                    return layer.msg("上传失败")
                }
                $("#demo1").attr("src",res.data.src)
                $("#photo").val(res.data.src)
            }
        })
        function productDelChk(obj) {
            var checkStatus=table.checkStatus(obj.config.id);
            var data=checkStatus.data
            if(data.length==0){
                layer.msg("请至少选择一个数据")
            }else {
                var productIds=new Array()
                $.each(data,function (index,item) {
                    productIds.push(item.productId)
                })
                layer.confirm("你确定要删除"+data.length+"条数据么",function (ind) {
                    $.post("/adminProduct/productDelChk",{"ids":productIds},function (r) {
                        layer.msg(r.msg)
                        productTable.reload()
                    })
                    layer.close(ind)
                })
            }
        }
        function del(data){


            layer.confirm("您确定要删除"+ data.productName +"商品么",function(ind){

                $.post("/adminProduct/delProduct", {"productId":data.productId} ,function(d) {
                    layer.msg(d.msg)
                    // 刷新table数据
                    productTable.reload()

                })
                layer.close(ind)
            })


        }
        //显示下拉框
        function initCategory() {
            $.post("/adminProduct/findCategory",function (res) {
                for(var i =0;i<res.data.length;i++){
                    $("[name='categoryId']").append("<option value=\""+res.data[i].categoryId+"\">"+res.data[i].categoryName+"</option>");
                }
                //重新渲染
                layui.form.render("select");

            },"json")
        }
        function menuUpdate(data) {

            index =  layer.open({
                type:1
                , title:"修改商品"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {

                    form.val("dataFrm",data)
                    //下拉选中
                    var select = 'dd[lay-value=' + data.categoryId + ']'
                    $('#categoryId').siblings("div.layui-form-select").find('dl').find(select).click();
                    $("#d1").show()
                    $("#demo1").attr("src",data.photo)
                    $("#productId").addClass("layui-btn-disabled")
                    url="/adminProduct/updateProduct"
                }
            })
        }
        function productAdd() {
            // 弹出框
            index =  layer.open({
                type:1 //
                , title:"添加商品"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    // 页面数据要清除
                    $("#dataFrm")[0].reset()
                    $("#demo1").attr("src","/images/image/moren.jpg")
                    $("#photo").val("/images/image/moren.jpg")
                    $("#d1").hide()
                    $("#productId").removeClass("layui-btn-disabled")
                    url="/adminProduct/addProduct"
                }
            })

        }
        // form表达提交
        form.on("submit(doSubmit)",function() {
            layer.confirm("您确定要提交该数据么",function(ind){
                $.post(url,$("#dataFrm").serialize(),function(data) {
                    layer.msg(data.msg)
                    layer.close(index)
                    productTable.reload()
                })
                layer.close(ind)
            })





        })
    })

</script>
</body>
</html>